package com.jieshuibao.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jieshuibao.model.db.Advisory;
import com.jieshuibao.model.db.Question;
import com.jieshuibao.model.db.RedEnvelopes;
import com.jieshuibao.model.db.Secretly;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.model.db.UserDealRecordExample;
import com.jieshuibao.model.db.UserDealRecordExample.Criteria;
import com.jieshuibao.model.db.UserDealRecordExample.Criterion;
import com.jieshuibao.model.db.Wishbuy;
import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.model.domain.FlowRecordingResponse;
import com.jieshuibao.model.domain.PageRequest;
import com.jieshuibao.service.AdvisoryService;
import com.jieshuibao.service.QuestionService;
import com.jieshuibao.service.RedEnvelopesService;
import com.jieshuibao.service.RedisService;
import com.jieshuibao.service.SecretlyService;
import com.jieshuibao.service.UserDealRecordService;
import com.jieshuibao.service.UserService;
import com.jieshuibao.service.WishService;
import com.jieshuibao.service.WishbuyService;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.StringUtils;

@Controller
@RequestMapping(value = "/flow")
public class RunningAccountController {
	private static final Logger logger = LoggerFactory
			.getLogger("RunningAccountController.class");

	private static final ObjectMapper mapper = new ObjectMapper();

	@Resource
	private RedisService redisService;
	
	@Resource
	private UserDealRecordService userDealRecordService;
	
	@Resource
	private QuestionService questionService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private SecretlyService secretlyService;
	
	@Resource
	private RedEnvelopesService redEnvelopesService;
	
	@Resource
	private WishService wishService;
	
	@Resource
	private WishbuyService wishbuyService;
	
	@Resource
	private AdvisoryService advisoryService;
	
	// 我的钱包    交易记录
	@RequestMapping(value = "/recording", method = RequestMethod.POST)
	@ResponseBody
	public Object envelopesRecording(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {
		FlowRecordingResponse flowRecordingResponse = new FlowRecordingResponse();

		try {
			logger.info("接收：" + mapper.writeValueAsString(pageRequest));

			CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					String phoneNumber = cookieUser.getPhoneNumber();
					if(phoneNumber!=null&&!phoneNumber.equals("")){
						int maxId = pageRequest.getMaxId();
						int sinceId = pageRequest.getSinceId();
						String tradeType = pageRequest.getArticleType();
						
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						UserDealRecordExample example = new UserDealRecordExample();
						example.setOrderByClause("paySucTime desc");
						example.setPageIndex(sinceId - 1);
						example.setPageSize(maxId - sinceId + 1);
						Criteria criteria = example.createCriteria();
						criteria.andStatusEqualTo("0");
						criteria.andSumNotEqualTo(0.0);
						criteria.andUserIdEqualTo(phoneNumber);
						if(tradeType!=null&&!tradeType.equals("")){
							if(tradeType.equals("income")){
								criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_INCOME);
							}
							
							if(tradeType.equals("outlay")){
								criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_EXPENDITURE);
							}
							
							List<UserDealRecord> userDealRecord = userDealRecordService.getPageUserDealRecord(example);
							
							if(userDealRecord!=null){
								
								for (UserDealRecord dealRecord : userDealRecord) {
									String goodsType = dealRecord.getGoodsType();
									String tradeType2 = dealRecord.getTradeType();
									String businessNo = dealRecord.getBusinessNo();
									
									if(goodsType.equals(PayTypeUtil.GOODSTYPE_REWARD)){
										//悬赏流水
										Question question = questionService.getQuestionByUuid(businessNo);
										String phoneNum = question.getPhoneNum();
										
										if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
											User user = userService.getUserByPhoneNumber(phoneNum);
											if (user==null) {
												dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_1+PayTypeUtil.REWARD_DESCRIPTION_2);
											}else {
												String realName = user.getRealName();
												String phone = user.getPhone();
												
												dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
												if(realName!=null&&!realName.equals("")){
													
													dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_1+realName+PayTypeUtil.REWARD_DESCRIPTION_2);
												}else {
													dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_1+phone+PayTypeUtil.REWARD_DESCRIPTION_2);
												}
											}
											
										}else {
											dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_0);
										}
										
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_SECRETLY)){
										//偷偷看
										Secretly secretly = secretlyService.getSecretlyByUuid(businessNo);
										String peepPhoneNum = secretly.getPeepPhoneNum();
										User user = userService.getUserByPhoneNumber(peepPhoneNum);
										
										if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
											//收入
											dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
											if(user!=null){
												String realName = user.getRealName();
												String phone = user.getPhone();
												
												if(realName!=null&&!realName.equals("")){
													dealRecord.setGoodsType(PayTypeUtil.SECRETLY_DESCRIPTION_1+realName+PayTypeUtil.SECRETLY_DESCRIPTION_2);
												}else {
													dealRecord.setGoodsType(PayTypeUtil.SECRETLY_DESCRIPTION_1+phone+PayTypeUtil.SECRETLY_DESCRIPTION_2);
												}
											}else {
												dealRecord.setGoodsType("此数据信息用户信息错误，请联系管理员");
											}
											
										}else{
											dealRecord.setGoodsType(PayTypeUtil.SECRETLY_DESCRIPTION_0);
										}
										
										
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_VIPCARD)){
										//会员卡
										
										if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
											//会员卡收入  个人 会员卡  企业会员卡
											dealRecord.setGoodsType(PayTypeUtil.VIPCARD_DESCRIPTION_0);
											dealRecord.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
										}else {
											//支出 会员卡充值  余额 和微信支付
											dealRecord.setGoodsType(PayTypeUtil.VIPCARD_DESCRIPTION_1);
										}
										
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_WITHIN)){
										//内购
										dealRecord.setGoodsType(PayTypeUtil.WITHIN_DESCRIPTION_0);
										dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
										
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ENVELOPES)){
										//红包
										RedEnvelopes redEnvelopes = redEnvelopesService.getRedEnvelopesByUuid(businessNo);
										if(redEnvelopes!=null){
											
											String redType = redEnvelopes.getRedType();
											if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
												//收入  偷偷看 红包 被别人打赏  分享 注册 
//												dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
												if(redType.equals(PayTypeUtil.REDENVELPES_TTK)){
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_0);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_REWARD)){
													//被别人打赏
													String sendPhoneNum = redEnvelopes.getSendPhoneNum();
													User user = userService.getUserByPhoneNumber(sendPhoneNum);
													if(user!=null){
														String realName = user.getRealName();
														String phone = user.getPhone();
														
														if(realName!=null&&!realName.equals("")){
															dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_1+realName+PayTypeUtil.ENVELOPES_DESCRIPTION_2);
														}else {
															dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_1+phone+PayTypeUtil.ENVELOPES_DESCRIPTION_2);
														}
													}
												}else if(redType.equals(PayTypeUtil.REDENVELPES_REGISTER)){
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_3);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE)){
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_4);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_NEWYEAR)){
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_6);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_MEDAL)){
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_7);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_REGISTER_QACARD)
														|| redType.equals(PayTypeUtil.REDENVELPES_BEINVITER)){//注册赠送红包
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_8);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_BINDINGQACARD)){//绑定问答卡红包
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_9);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_INVITER)){//邀请好友奖励
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_10);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE_ARTICLE)){//分享文章红包
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_11);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE_SECRETLY)){//分享围观红包
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_12);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE_QUESTION)){//分享被采纳的提问
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_13);
												}else if(redType.equals(PayTypeUtil.REDENVELPES_PERSONAL_INFO)){//补全个人资料
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_14);
												}else{
													dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_0);
												}
												
											}else {
												//支出 打赏 给别人 
												String userPhoneNum = redEnvelopes.getUserPhoneNum();
												User user = userService.getUserByPhoneNumber(userPhoneNum);
												if(user!=null){
													String realName = user.getRealName();
													String phone = user.getPhone();
													
													if(realName!=null&&!realName.equals("")){
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_5+realName);
													}else {
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_4+phone);
													}
												}
												
											}
										}else {
											//数据错误
											flowRecordingResponse.setCode("3");
											flowRecordingResponse.setDes("数据错误！！");
											return flowRecordingResponse;
										}
										
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_WISHS)){
										dealRecord.setGoodsType("许愿");
										
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_VIDEOS)){
										dealRecord.setGoodsType("视频");
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADDREWARD)){
										dealRecord.setGoodsType("加赏");
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADVISORY)){
										if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
											dealRecord.setGoodsType("咨询收入");
										}else {
											dealRecord.setGoodsType("咨询费用");
										}
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADVISORYBACK)){
										dealRecord.setGoodsType("咨询费用退款");
										
									}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_REWARDBACK)){
										dealRecord.setGoodsType("悬赏费用退回");
									}else if (goodsType.equals(PayTypeUtil.GOODSTYPE_VIDEO_CARD)) {
										dealRecord.setGoodsType("视频");
									}else if (goodsType.equals(PayTypeUtil.GOODSTYPE_VIDEO_SINGLE)) {
										dealRecord.setGoodsType("视频");
									}else if (goodsType.equals(PayTypeUtil.GOODSTYPE_BALANCE_RECHARGE)) {
										dealRecord.setGoodsType("钱包充值");
									}else{
										//商品类型错误
										flowRecordingResponse.setCode("3");
										flowRecordingResponse.setDes("商品类型错误！！");
										return flowRecordingResponse;
									}
									
								}
								
								flowRecordingResponse.setCode("1");
								flowRecordingResponse.setRecords(userDealRecord);
								flowRecordingResponse.setMaxId(sinceId + userDealRecord.size()- 1);
								flowRecordingResponse.setSinceId(sinceId);
								flowRecordingResponse.setRows(userDealRecord.size());
								
							}
						}else {
							flowRecordingResponse.setCode("3");
							flowRecordingResponse.setDes("数据格式错误！！");
						}
						
						
						
					}else {
						// 重新登录
						flowRecordingResponse.setCode("2");
						flowRecordingResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					flowRecordingResponse.setCode("2");
					flowRecordingResponse.setDes("重新登录");
				}
			
			logger.info("返回："
					+ mapper.writeValueAsString(flowRecordingResponse));
		} catch (Exception e) {
			e.printStackTrace();
			flowRecordingResponse.setCode("0");
			flowRecordingResponse.setDes("系统繁忙");
		}
		return flowRecordingResponse;
	}
	
	
	// 我的钱包   交易记录   增加 收入 支出 的合计
		@RequestMapping(value = "/sumRecording", method = RequestMethod.POST)
		@ResponseBody
		public Object tradeRecording(HttpServletRequest request,
				@RequestBody PageRequest pageRequest) {
			FlowRecordingResponse flowRecordingResponse = new FlowRecordingResponse();

			try {
				logger.info("接收：" + mapper.writeValueAsString(pageRequest));
				//接收  记录类型（收入 支出  全部）
				//   支付类型 （我的顾问卡， 财税问答卡，钱包）
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
					if (cookieUser != null) {
						String phoneNumber = cookieUser.getPhoneNumber();
						if(phoneNumber!=null&&!phoneNumber.equals("")){
							int maxId = pageRequest.getMaxId();
							int sinceId = pageRequest.getSinceId();
							String tradeType = pageRequest.getArticleType();
							String payTypeRequest = pageRequest.getPayType();
							
							if (sinceId <= 0) {
								sinceId = 1;
							}
							if (maxId <= 0 || maxId < sinceId) {
								maxId = sinceId;
							}
							UserDealRecordExample example = new UserDealRecordExample();
							example.setOrderByClause("paySucTime desc");
							example.setPageIndex(sinceId - 1);
							example.setPageSize(maxId - sinceId + 1);
							Criteria criteria = example.createCriteria();
							criteria.andStatusEqualTo("0");
							criteria.andSumNotEqualTo(0.0);
							criteria.andUserIdEqualTo(phoneNumber);
							
							if(StringUtils.isBlank(payTypeRequest)){
								flowRecordingResponse.setCode("3");
								flowRecordingResponse.setDes("数据错误！！");
								return flowRecordingResponse;
							}
							
							if(payTypeRequest.equals("studyCard")){
								criteria.andPayTypeEqualTo(PayTypeUtil.PAYTYPE_VIPBALANCE);
								
							}else if(payTypeRequest.equals("qaCard")){
								criteria.andPayTypeEqualTo(PayTypeUtil.PAYTYPE_QACARDBALANCE);
							}else{
								criteria.andPayTypeNotEqualTo(PayTypeUtil.PAYTYPE_VIPBALANCE).andPayTypeNotEqualTo(PayTypeUtil.PAYTYPE_QACARDBALANCE);
							}
								
							
							
							
							if(tradeType!=null&&!tradeType.equals("")){
								if(tradeType.equals("income")){
									criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_INCOME);
								}
								
								if(tradeType.equals("outlay")){
									criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_EXPENDITURE);
								}
								
								List<UserDealRecord> userDealRecord = userDealRecordService.getPageUserDealRecord(example);
								
								if(userDealRecord!=null){
									for (UserDealRecord dealRecord : userDealRecord) {
										String goodsType = dealRecord.getGoodsType();
										String tradeType2 = dealRecord.getTradeType();
										String businessNo = dealRecord.getBusinessNo();
										String payType = dealRecord.getPayType();
										
										if(goodsType.equals(PayTypeUtil.GOODSTYPE_REWARD)){
											//悬赏流水
											Question question = questionService.getQuestionByUuid(businessNo);
											String phoneNum = question.getPhoneNum();
											
											if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
												User user = userService.getUserByPhoneNumber(phoneNum);
												if (user==null) {
													dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_1+PayTypeUtil.REWARD_DESCRIPTION_2);
												}else {
													String realName = user.getRealName();
													String phone = user.getPhone();
													
													dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
													if(realName!=null&&!realName.equals("")){
														
														dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_1+realName+PayTypeUtil.REWARD_DESCRIPTION_2);
													}else {
														dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_1+phone+PayTypeUtil.REWARD_DESCRIPTION_2);
													}
												}
												
											}else {
												dealRecord.setGoodsType(PayTypeUtil.REWARD_DESCRIPTION_0);
											}
											
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_SECRETLY)){
											//偷偷看
											Secretly secretly = secretlyService.getSecretlyByUuid(businessNo);
											String peepPhoneNum = secretly.getPeepPhoneNum();
											User user = userService.getUserByPhoneNumber(peepPhoneNum);
											
											if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
												//收入
												dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
												if(user!=null){
													String realName = user.getRealName();
													String phone = user.getPhone();
													
													if(realName!=null&&!realName.equals("")){
														dealRecord.setGoodsType(PayTypeUtil.SECRETLY_DESCRIPTION_1+realName+PayTypeUtil.SECRETLY_DESCRIPTION_2);
													}else {
														dealRecord.setGoodsType(PayTypeUtil.SECRETLY_DESCRIPTION_1+phone+PayTypeUtil.SECRETLY_DESCRIPTION_2);
													}
												}else {
													dealRecord.setGoodsType("此数据信息用户信息错误，请联系管理员");
												}
												
											}else{
												dealRecord.setGoodsType(PayTypeUtil.SECRETLY_DESCRIPTION_0);
											}
											
											
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_VIPCARD)){
											//会员卡
											
											if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
												//会员卡收入  个人 会员卡  企业会员卡
												dealRecord.setGoodsType(PayTypeUtil.VIPCARD_DESCRIPTION_0);
												dealRecord.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
											}else {
												//支出 会员卡充值  余额 和微信支付
												dealRecord.setGoodsType(PayTypeUtil.VIPCARD_DESCRIPTION_1);
											}
											
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_WITHIN)){
											//内购
											dealRecord.setGoodsType(PayTypeUtil.WITHIN_DESCRIPTION_0);
											dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
											
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ENVELOPES)){
											//红包
											RedEnvelopes redEnvelopes = redEnvelopesService.getRedEnvelopesByUuid(businessNo);
											if(redEnvelopes!=null){
												
												String redType = redEnvelopes.getRedType();
												if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
													//收入  偷偷看 红包 被别人打赏  分享 注册 
//													dealRecord.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
													if(redType.equals(PayTypeUtil.REDENVELPES_TTK)){
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_0);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_REWARD)){
														//被别人打赏
														String sendPhoneNum = redEnvelopes.getSendPhoneNum();
														User user = userService.getUserByPhoneNumber(sendPhoneNum);
														if(user!=null){
															String realName = user.getRealName();
															String phone = user.getPhone();
															
															if(realName!=null&&!realName.equals("")){
																dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_1+realName+PayTypeUtil.ENVELOPES_DESCRIPTION_2);
															}else {
																dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_1+phone+PayTypeUtil.ENVELOPES_DESCRIPTION_2);
															}
														}
													}else if(redType.equals(PayTypeUtil.REDENVELPES_REGISTER)){
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_3);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE)){
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_4);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_NEWYEAR)){
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_6);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_MEDAL)){
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_7);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_REGISTER_QACARD)
															|| redType.equals(PayTypeUtil.REDENVELPES_BEINVITER)){//注册赠送红包
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_8);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_BINDINGQACARD)){//绑定问答卡红包
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_9);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_INVITER)){//邀请好友奖励
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_10);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE_ARTICLE)){//分享文章红包
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_11);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE_SECRETLY)){//分享围观红包
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_12);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_SHARE_QUESTION)){//分享被采纳的提问
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_13);
													}else if(redType.equals(PayTypeUtil.REDENVELPES_PERSONAL_INFO)){//补全个人资料
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_14);
													}else{
														dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_0);
													}
													
												}else {
													//支出 打赏 给别人 
													String userPhoneNum = redEnvelopes.getUserPhoneNum();
													User user = userService.getUserByPhoneNumber(userPhoneNum);
													if(user!=null){
														String realName = user.getRealName();
														String phone = user.getPhone();
														
														if(realName!=null&&!realName.equals("")){
															dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_5+realName);
														}else {
															dealRecord.setGoodsType(PayTypeUtil.ENVELOPES_DESCRIPTION_4+phone);
														}
													}
													
												}
											}else {
												//数据错误
												flowRecordingResponse.setCode("3");
												flowRecordingResponse.setDes("数据错误！！");
												return flowRecordingResponse;
											}
											
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_WISHS)){
											dealRecord.setGoodsType("许愿");
											
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_VIDEOS)){
											dealRecord.setGoodsType("视频");
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADDREWARD)){
											dealRecord.setGoodsType("加赏");
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADVISORY)){
											if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
												dealRecord.setGoodsType("咨询收入");
											}else {
												dealRecord.setGoodsType("咨询费用");
											}
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADVISORYBACK)){
											dealRecord.setGoodsType("咨询费用退款");
											
										}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_REWARDBACK)){
											dealRecord.setGoodsType("悬赏费用退回");
										}else if (goodsType.equals(PayTypeUtil.GOODSTYPE_VIDEO_CARD)) {
											dealRecord.setGoodsType("视频");
										}else if (goodsType.equals(PayTypeUtil.GOODSTYPE_VIDEO_SINGLE)) {
											dealRecord.setGoodsType("视频");
										}else if (goodsType.equals(PayTypeUtil.GOODSTYPE_BALANCE_RECHARGE)) {
											dealRecord.setGoodsType("钱包充值");
										}else{
											//商品类型错误
											flowRecordingResponse.setCode("3");
											flowRecordingResponse.setDes("商品类型错误！！");
											return flowRecordingResponse;
										}
										
									}
									
									//根据 支付类型   计算 收入 、 支出  的合计
									
									if(tradeType.equals("all")){
										/*if(payTypeRequest.equals(PayTypeUtil.PAYTYPE_VIPBALANCE)){  //支付类型  是  我的学习顾问卡
											
										}*/
										List<Criterion> criteria2 = criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_INCOME).getCriteria();
										Double income = userDealRecordService.getSumIncome(example);  //2  收入合计
										criteria2.remove(criteria2.size()-1);
								
										criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_EXPENDITURE);
									
										Double outLay = userDealRecordService.getSumIncome(example);  //1 支出合计
									
										flowRecordingResponse.setIncomeSum(income);
										flowRecordingResponse.setOutpaySum(outLay);
									}
									
									flowRecordingResponse.setCode("1");
									flowRecordingResponse.setRecords(userDealRecord);
									flowRecordingResponse.setMaxId(sinceId + userDealRecord.size()- 1);
									flowRecordingResponse.setSinceId(sinceId);
									flowRecordingResponse.setRows(userDealRecord.size());
									
								}
							}else {
								flowRecordingResponse.setCode("3");
								flowRecordingResponse.setDes("数据格式错误！！");
							}
							
							
							
						}else {
							// 重新登录
							flowRecordingResponse.setCode("2");
							flowRecordingResponse.setDes("重新登录");
						}
					} else {
						// 重新登录
						flowRecordingResponse.setCode("2");
						flowRecordingResponse.setDes("重新登录");
					}
				
				logger.info("返回："
						+ mapper.writeValueAsString(flowRecordingResponse));
			} catch (Exception e) {
				e.printStackTrace();
				flowRecordingResponse.setCode("0");
				flowRecordingResponse.setDes("系统繁忙");
			}
			return flowRecordingResponse;
		}
		
		
		
					
		
	
	@RequestMapping(value = "/company", method = RequestMethod.POST)
	@ResponseBody
	public Object companyRecording(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {
		FlowRecordingResponse flowRecordingResponse = new FlowRecordingResponse();

		try {
			logger.info("接收：" + mapper.writeValueAsString(pageRequest));

				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					String phoneNumber = cookieUser.getPhoneNumber();
					if(phoneNumber!=null&&!phoneNumber.equals("")){
						System.out.println("phoneNumber: "+phoneNumber);
						int maxId = pageRequest.getMaxId();
						int sinceId = pageRequest.getSinceId();
						String tradeType = pageRequest.getArticleType();
						
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						UserDealRecordExample example = new UserDealRecordExample();
						example.setOrderByClause("paySucTime desc");
						example.setPageIndex(sinceId - 1);
						example.setPageSize(maxId - sinceId + 1);
						Criteria criteria = example.createCriteria();
						criteria.andStatusEqualTo("0");
						
						User user = userService.getUserByPhoneNumber(phoneNumber);
						if(user!=null){
							int companyRole = user.getCompanyRole();
							if(companyRole!=0){
								String companyUuid = user.getCompanyUuid();
								criteria.andUserIdEqualTo(companyUuid);
								if(tradeType!=null&&tradeType.equals("income")){
									criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_INCOME);
								}else {
									criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_EXPENDITURE);

								}
								List<UserDealRecord> userDealRecord = userDealRecordService.getPageUserDealRecord(example);
								if(userDealRecord!=null){
									for (UserDealRecord userRecord : userDealRecord) {
										String tradeType2 = userRecord.getTradeType();
										String businessNo = userRecord.getBusinessNo();
										String payType = userRecord.getPayType();
										if(tradeType2.equals(PayTypeUtil.TRDETYPE_INCOME)){
											//充值
											if(payType.equals(PayTypeUtil.PAYTYPE_VIPBALANCE)){
												//合并
												User user2 = userService.getUserByPhoneNumber(businessNo);
												if(user2!=null){
													String realName = user2.getRealName();
													String phone = user2.getPhone();
													
													if(realName!=null&&!realName.equals("")){
														userRecord.setGoodsType(realName+PayTypeUtil.COMPANYCARD_VIPCARD_0);
													}else {
														userRecord.setGoodsType(phone+PayTypeUtil.COMPANYCARD_VIPCARD_0);
													}
												}else {
													//数据格式错误
													userRecord.setGoodsType("这条数据有问题 ，仔细检查一下");
												}
											}else {
												//购买
												userRecord.setGoodsType(PayTypeUtil.COMPANYCARD_VIPCARD_1);
											}
											
										}else {
											//支出
											if(payType.equals(PayTypeUtil.PAYTYPE_COMPANYBALANCE)){
												String goodsType = userRecord.getGoodsType();
												if(goodsType.equals(PayTypeUtil.GOODSTYPE_REWARD)){
													//悬赏
													Question question = questionService.getQuestionByUuid(businessNo);
													if(question!=null){
														
														String phoneNum = question.getPhoneNum();
														User user2 = userService.getUserByPhoneNumber(phoneNum);
														if(user2!=null){
															String realName = user2.getRealName();
															String phone = user2.getPhone();
															if(realName!=null&&!realName.equals("")){
																userRecord.setGoodsType(realName+PayTypeUtil.COMPANYCARD_REWARD_0);
																
															}else {
																userRecord.setGoodsType(phone+PayTypeUtil.COMPANYCARD_REWARD_0);
															}
														}else {
															//这条数据有问题
															userRecord.setGoodsType("这条数据有问题，没有找到人");
														}
													}else {
														//这条数据有问题
														userRecord.setGoodsType("这条数据有问题，没有找到问题");
													}
													
													
												}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_SECRETLY)){
													//偷偷看
													Secretly secretly = secretlyService.getSecretlyByUuid(businessNo);
													if(secretly!=null){
														String peepPhoneNum = secretly.getPeepPhoneNum();
														User user2 = userService.getUserByPhoneNumber(peepPhoneNum);
														if(user2!=null){
															String realName = user2.getRealName();
															String phone = user2.getPhone();
															
															if(realName!=null&&!realName.equals("")){
																userRecord.setGoodsType(realName+PayTypeUtil.COMPANYCARD_SECRETLY_0);
															}else {
																userRecord.setGoodsType(phone+PayTypeUtil.COMPANYCARD_SECRETLY_0);
															}
														}else {
															//数据有问题
															userRecord.setGoodsType("偷偷这条数据有问题，没有找到人");
														}
														
													}else {
														//数据有问题
														userRecord.setGoodsType("偷偷这条数据有问题，");
													}
													
												}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ENVELOPES)){
													RedEnvelopes redEnvelopes = redEnvelopesService.getRedEnvelopesByUuid(businessNo);
													if(redEnvelopes!=null&&!redEnvelopes.equals("")){
														String sendPhoneNum = redEnvelopes.getSendPhoneNum();
														User user2 = userService.getUserByPhoneNumber(sendPhoneNum);
														if(user2!=null){
															String realName = user2.getRealName();
															String phone = user2.getPhone();
															
															if(realName!=null&&!realName.equals("")){
																userRecord.setGoodsType(realName+PayTypeUtil.COMPANYCARD_ENVELOPES_0);
															}else {
																userRecord.setGoodsType(phone+PayTypeUtil.COMPANYCARD_ENVELOPES_0);
															}
														}else {
															//数据有问题
															userRecord.setGoodsType("这条文章打赏数据有问题，没有找到人");
														}
														
													}else{
														//数据有问题
														userRecord.setGoodsType("这条文章打赏数据有问题");
													}
														
													
												}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_WISHS)){
													
													Wishbuy wishbuyById = wishbuyService.getWishbuyById(businessNo);
													String userId = wishbuyById.getUserId();
													User user2 = userService.getUserByPhoneNumber(userId);
													if(user2!=null){
														String realName = user2.getRealName();
														String phone = user2.getPhone();
														
														if(realName!=null&&!realName.equals("")){
															userRecord.setGoodsType(realName+"许愿");
														}else {
															userRecord.setGoodsType(phone+"许愿");
														}
													}else {
														userRecord.setGoodsType("许愿这条数据有问题，没有找到人");
													}
												}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_VIDEOS)){
													
													userRecord.setGoodsType("视频");
												}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADDREWARD)){
													Question question = questionService.getQuestionByUuid(businessNo);
													if(question!=null){
														
														String phoneNum = question.getPhoneNum();
														User user2 = userService.getUserByPhoneNumber(phoneNum);
														if(user2!=null){
															String realName = user2.getRealName();
															String phone = user2.getPhone();
															
															if(realName!=null&&!realName.equals("")){
																userRecord.setGoodsType(realName+"加赏");
																
															}else {
																userRecord.setGoodsType(phone+"加赏");
															}
														}else {
															//这条数据有问题
															userRecord.setGoodsType("数据异常，请联系管理员");
														}
													}else {
														//这条数据有问题
														userRecord.setGoodsType("数据异常，请联系管理员");
													}
												}else if(goodsType.equals(PayTypeUtil.GOODSTYPE_ADVISORY)){
													Advisory advisory = advisoryService.getAdvisoryByUuid(businessNo);
													if(advisory!=null){
														
														String userAsk = advisory.getUserAsk();
														User user2 = userService.getUserByPhoneNumber(userAsk);
														if(user2!=null){
															String realName = user2.getRealName();
															String phone = user2.getPhone();
															
															if(realName!=null&&!realName.equals("")){
																userRecord.setGoodsType(realName+"咨询");
															}else {
																userRecord.setGoodsType(phone+"咨询");
															}
														}
													}
													
												}else{
													//那就是数据有问题
													userRecord.setGoodsType("商品类型错误，请联系管理员");
												}
											}else {
												//数据有问题
												userRecord.setGoodsType("这条数据存的支付方式错了");
											}
											
										}
									}	
									flowRecordingResponse.setRecords(userDealRecord);
									flowRecordingResponse.setMaxId(sinceId + userDealRecord.size()- 1);
									flowRecordingResponse.setSinceId(sinceId);
									flowRecordingResponse.setRows(userDealRecord.size());
									flowRecordingResponse.setCode("1");
								}
							}else {
								//
								flowRecordingResponse.setCode("3");
								flowRecordingResponse.setDes("你没有权限");
							}
						}else {
							//重新登录
							flowRecordingResponse.setCode("2");
							flowRecordingResponse.setDes("重新登录");
						}
						
					}else {
						// 重新登录
						flowRecordingResponse.setCode("2");
						flowRecordingResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					flowRecordingResponse.setCode("2");
					flowRecordingResponse.setDes("重新登录");
				}
			
			logger.info("返回："
					+ mapper.writeValueAsString(flowRecordingResponse));
		} catch (Exception e) {
			e.printStackTrace();
			flowRecordingResponse.setCode("0");
			flowRecordingResponse.setDes("系统繁忙");
		}
		return flowRecordingResponse;
	}
}
